#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

//全局变量，所有线程共享这一份资源
int tickets = 2000;
//创建一个互斥量
pthread_mutex_t mutex;

void * sellticket(void * arg) {
    //卖票
    while (1)
    {
        pthread_mutex_lock(&mutex);
        if(tickets > 0) {
            usleep(6000);
            printf("%ld 正在卖第 %d 张门票\n", pthread_self(), tickets);
            tickets--;  
            pthread_mutex_unlock(&mutex);
            usleep(1);
        } else {
            pthread_mutex_unlock(&mutex);
            break;
        }
        //解锁
    }
    return NULL;
}

int main() {

    //初始化互斥量
    pthread_mutex_init(&mutex, NULL);

    //创建三个线程
    pthread_t tid1, tid2, tid3;
    pthread_create(&tid1, NULL,sellticket, NULL);
    pthread_create(&tid2, NULL,sellticket, NULL);
    pthread_create(&tid3, NULL,sellticket, NULL);

    //回收子线程的资源,是阻塞的
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    pthread_join(tid3, NULL);

    //释放互斥量资源
    pthread_mutex_destroy(&mutex);

    return 0;
}

